Windows Capture  
Windows Capture is a highly efficient Rust and Python library that enables you to capture the screen using the Graphics Capture API effortlessly. This library allows you to easily capture the screen of your Windows-based computer and use it for various purposes, such as creating instructional videos, taking screenshots, or recording your gameplay. With its intuitive interface and robust functionality, Windows Capture is an excellent choice for anyone looking for a reliable, easy-to-use screen-capturing solution.
Note this README.md is for Rust library Python library can be found here
Features
- Only Updates The Frame When Required.
- High Performance.
- Easy To Use.
- Latest Screen Capturing API.
Installation
Add this library to your Cargo.toml
:
[dependencies]
windows-capture = "1.2.0"
or run this command
cargo add windows-capture
Usage
use std::{
io::{self, Write},
time::Instant,
};
use windows_capture::{
capture::GraphicsCaptureApiHandler,
encoder::{VideoEncoder, VideoEncoderQuality, VideoEncoderType},
frame::Frame,
graphics_capture_api::InternalCaptureControl,
monitor::Monitor,
settings::{ColorFormat, CursorCaptureSettings, DrawBorderSettings, Settings},
};
struct Capture {
encoder: Option<VideoEncoder>,
start: Instant,
}
impl GraphicsCaptureApiHandler for Capture {
type Flags = String;
type Error = Box<dyn std::error::Error + Send + Sync>;
fn new(message: Self::Flags) -> Result<Self, Self::Error> {
println!("Got The Flag: {message}");
let encoder = VideoEncoder::new(
VideoEncoderType::Mp4,
VideoEncoderQuality::HD1080p,
1920,
1080,
"video.mp4",
)?;
Ok(Self {
encoder: Some(encoder),
start: Instant::now(),
})
}
fn on_frame_arrived(
&mut self,
frame: &mut Frame,
capture_control: InternalCaptureControl,
) -> Result<(), Self::Error> {
print!(
"\rRecording for: {} seconds",
self.start.elapsed().as_secs()
);
io::stdout().flush()?;
self.encoder.as_mut().unwrap().send_frame(frame)?;
if self.start.elapsed().as_secs() >= 6 {
self.encoder.take().unwrap().finish()?;
capture_control.stop();
println!();
}
Ok(())
}
fn on_closed(&mut self) -> Result<(), Self::Error> {
println!("Capture Session Closed");
Ok(())
}
}
fn main() {
let primary_monitor = Monitor::primary().expect("There is no primary monitor");
let settings = Settings::new(
primary_monitor,
CursorCaptureSettings::Default,
DrawBorderSettings::Default,
ColorFormat::Rgba8,
"Yea This Works".to_string(),
);
Capture::start(settings).expect("Screen Capture Failed");
}
Documentation
Detailed documentation for each API and type can be found here.
Contributing
Contributions are welcome! If you find a bug or want to add new features to the library, please open an issue or submit a pull request.
License
This project is licensed under the MIT License.